<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="产品名称" v-model="listQuery.ProductName">
      </el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="float:right" @click="handleCreate" type="primary" icon="edit">添加</el-button>
      <el-button class="filter-item" style="float:right" type="primary" icon="document" @click="handleDownload">导出</el-button>
    </div>

    <el-table :key='tableKey' 
    :data="list"  
    @sort-change = "sortChange"
    border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" fixed sortable="custom" prop="id" label="序号" width="100px">
        <template scope="scope">
          <span>{{scope.row.id}}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="100px" label="名称">
        <template scope="scope">
          <span class="link-type">{{scope.row.ProductName}}</span>
        </template>
      </el-table-column>

      <el-table-column width="150px" align="center" label="key">
        <template scope="scope">
          <span>{{scope.row.ProductKey}}</span>
        </template>
      </el-table-column>

      <el-table-column width="80px" label="分类Id">
        <template scope="scope">
           <span>{{scope.row.CatId}}</span>
        </template>
      </el-table-column>

      <el-table-column  align="center" label="描述">
        <template scope="scope">
          <span>{{scope.row.ProductDesc}}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px"  sortable prop="created_at" align="center" label="时间">
        <template scope="scope" >
          <span>{{scope.row.created_at }}</span>
        </template>
      </el-table-column>

      <el-table-column width="200px" fixed="right" align="center" label="操作" >
        <template scope="scope">
          <el-button  size="small" type="success" @click="handleView(scope.row)">查看
          </el-button>
          <el-button size="small" type="success" @click="handleUpdate(scope.row)">编辑
          </el-button>
          </el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>

    <div class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[10,20,30, 50]" :page-size="listQuery.page_limit" layout="total, sizes, prev, pager, next, jumper" :total="listQuery.total">
      </el-pagination>
    </div>

    <el-dialog :title="ProductDailog.textMap[ProductDailog.dialogStatus]" :visible.sync="ProductDailog.visible">
      <el-form class="small-space" :model="productForm" label-position="left" label-width="70px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="产品名称">
          <el-input v-model="productForm.ProductName" placeholder="输入产品名称"></el-input>
        </el-form-item>

        <el-form-item label="类型Id">
          <el-input v-model="productForm.CatId" placeholder="输入类型id"></el-input>
        </el-form-item>

        <el-form-item label="产品描述">
          <el-input v-model="productForm.ProductDesc" placeholder="输如产品描述"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="ProductDailog.visible = false">取 消</el-button>
        <el-button v-if="ProductDailog.dialogStatus=='create'" type="primary" @click="create">确 定</el-button>
        <el-button v-else type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除提示 -->
    <el-dialog
      title="提示"
      :visible.sync="delProductDialogVisible"
      width="30%">
      <span>确定要删除该产品吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delProductDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delProduct">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {
  getProductList,
  addProduct,
  updateProduct,
  deleteProduct
} from "@/api/product";
import waves from "@/directive/waves/index.js"; // 水波纹指令
import { parseTime } from "@/utils";

export default {
  name: "table_product",
  directives: {
    waves
  },
  data() {
    return {
      list: null,
      listQuery: {
        page_limit: 10,
        total: 0,
        page: 1
      },
      productForm: {
        ProductName: "",
        CatId: "",
        ProductDesc: ""
      },

      //产品弹窗
      ProductDailog: {
        visible: false,
        dialogStatus: "",
        textMap: {
          update: "编辑",
          create: "创建"
        }
      },
      //删除产品
      cur_row: null,
      delProductDialogVisible: false,

      tableKey: 0
    };
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type];
    }
  },
  created() {
    this.getList(this.listQuery);
  },
  methods: {
    getList(params) {
      getProductList(params).then(r => {
        this.list = r.data.data;
        this.listQuery.total = r.data.total;
        this.listQuery.page = r.data.current_page;
      });
    },
    sortChange(val) {
      if (val.order == "ascending") {
        this.listQuery.asc = val.prop;
        if(this.listQuery.desc==val.prop){
          this.listQuery.desc = null;
        }
      } else {
        this.listQuery.desc = val.prop;
        if(this.listQuery.asc==val.prop){
          this.listQuery.asc = null;
        }
      }
      this.getList(this.listQuery);
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList(this.listQuery);
    },
    handleSizeChange(val) {
      this.listQuery.page_limit = val;
      this.getList(this.listQuery);
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList(this.listQuery);
    },
    timeFilter(time) {
      if (!time[0]) {
        this.listQuery.start = undefined;
        this.listQuery.end = undefined;
        return;
      }
      this.listQuery.start = parseInt(+time[0] / 1000);
      this.listQuery.end = parseInt((+time[1] + 3600 * 1000 * 24) / 1000);
    },
    //创建
    handleCreate() {
      this.resetTemp();
      this.ProductDailog.dialogStatus = "create";
      this.ProductDailog.visible = true;
    },
    //查看
    handleView(row) {
      this.$router.push({
        path: "/product/device",
        query: {
          ProductKey: row.ProductKey
        }
      });
    },
    //更新
    handleUpdate(row) {
      this.resetTemp();
      this.productForm = Object.assign({}, row);
      this.ProductDailog.dialogStatus = "update";
      this.ProductDailog.visible = true;
    },
    //删除
    handleDelete(row) {
      this.delProductDialogVisible = true;
      this.cur_row = row;
    },
    create() {
      updateProduct(this.productForm).then(r => {
        å;
        this.getList(this.listQuery);
        this.$notify({
          title: "成功",
          message: "创建成功",
          type: "success",
          duration: 2000
        });
        this.ProductDailog.visible = false;
      });
    },
    update() {
      updateProduct(this.productForm.id, this.productForm).then(r => {
        this.getList(this.listQuery);
        this.$notify({
          title: "成功",
          message: "创建成功",
          type: "success",
          duration: 2000
        });
        this.ProductDailog.visible = false;
      });
    },
    delProduct() {
      deleteProduct(this.cur_row.id).then(r => {
        this.$notify({
          title: "成功",
          message: "删除成功",
          type: "success",
          duration: 2000
        });
        const index = this.list.indexOf(this.cur_row);
        this.list.splice(index, 1);
        this.cur_row = null;
        this.delProductDialogVisible = false;
      });
    },
    resetTemp() {
      this.productForm = {
        ProductName: "",
        CatId: "",
        ProductDesc: ""
      };
    },
    handleDownload() {
      require.ensure([], () => {
        const { export_json_to_excel } = require("../../vendor/Export2Excel");
        const tHeader = ["序号", "产品名称", "key", "分类Id", "描述", "时间"];
        const filterVal = [
          "id",
          "ProductName",
          "ProductKey",
          "CatId",
          "ProductDesc",
          "created_at"
        ];
        const data = this.formatJson(filterVal, this.list);
        export_json_to_excel(tHeader, data, "产品表");
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v =>
        filterVal.map(j => {
          if (j === "timestamp") {
            return parseTime(v[j]);
          } else {
            return v[j];
          }
        })
      );
    }
  }
};
</script>
